package cn.com.b2office.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.b2office.dao.UserDao;
import cn.com.b2office.domain.Member;
import cn.com.b2office.domain.MemberAddress;
import cn.com.b2office.domain.User;
import cn.com.b2office.util.MD5;

public class UserDaoHibernateImpl extends HibernateDaoSupport implements
		UserDao {

	public User loginByEmail(String email, String regPwd) {
		Member member = null;
		Session s = getSession();
		try {
			Query query = s.createQuery(
					"from Member where regemail=:email and regpwd=:regPwd")
					.setFirstResult(0).setMaxResults(1);
			query.setString("email", email);
			query.setString("regPwd", MD5.hash(regPwd));
			List<Member> list = query.list();
			if (list != null && !list.isEmpty())
				member = list.get(0);
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		if (member == null)
			return null;
		User user = new User();
		user.setMemberId(member.getMemberid());
		user.setMemberno(member.getMemberno());
		user.setRegName(member.getRegname());
		return user;
	}

	public User loginByMemberno(String memberno, String telPwd) {
		Member member = null;
		Session s = getSession();
		try {
			Query query = s.createQuery(
					"from Member where memberno=:memberno and numpwd=:telPwd")
					.setFirstResult(0).setMaxResults(1);
			query.setString("memberno", memberno);
			query.setString("telPwd", MD5.hash(telPwd));
			List<Member> list = query.list();
			if (list != null && !list.isEmpty())
				member = list.get(0);
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		if (member == null)
			return null;
		User user = new User();
		user.setMemberId(member.getMemberid());
		user.setMemberno(member.getMemberno());
		user.setRegName(member.getRegname());
		return user;
	}

	@SuppressWarnings("unchecked")
	public User loginByMobile(String mobile, String regPwd) {
		Member member = null;
		Session s = getSession();
		try {
			List<MemberAddress> list = s
					.createQuery(
							"from MemberAddress where flag=1 and deleted=0 and mobil=:mobile")
					.setFirstResult(0).setMaxResults(1).setString("mobile",
							mobile).list();
			if (list == null || list.isEmpty())
				return null;
			Query query = s
					.createQuery("from Member where memberid=:memberId and regpwd=:regPwd");
			query.setString("memberId", list.get(0).getMemberid());
			query.setString("regPwd", MD5.hash(regPwd));
			List<Member> members = query.list();
			if (members != null && !members.isEmpty())
				member = members.get(0);
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		if (member == null)
			return null;
		User user = new User();
		user.setMemberId(member.getMemberid());
		user.setMemberno(member.getMemberno());
		user.setRegName(member.getRegname());
		return user;
	}

	public User loginByRegName(String regName, String regPwd) {
		Member member = null;
		Session s = getSession();
		try {
			Query query = s.createQuery(
					"from Member where regname=:regName and regpwd=:regPwd")
					.setFirstResult(0).setMaxResults(1);
			query.setString("regName", regName);
			query.setString("regPwd", MD5.hash(regPwd));
			List<Member> list = query.list();
			if (list != null && !list.isEmpty())
				member = list.get(0);
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		if (member == null)
			return null;
		User user = new User();
		user.setMemberId(member.getMemberid());
		user.setMemberno(member.getMemberno());
		user.setRegName(member.getRegname());
		return user;
	}

}
